<!DOCTYPE html><html lang="en"><head><title>RefundSale</title></head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0"><meta name="groc-relative-root" content=""><meta name="groc-document-path" content="RefundSale"><meta name="groc-project-path" content="RefundSale.php"><link rel="stylesheet" type="text/css" media="all" href="assets/style.css"><script type="text/javascript" src="assets/behavior.js"></script><body><div id="meta"><div class="file-path">RefundSale.php</div></div><div id="document"><div class="segment"><div class="code"><div class="wrapper"><span class="cp">&lt;?php</span></div></div></div><div class="segment"><div class="comments"><div class="wrapper"><h1 id="sale-refund-sample">Sale Refund Sample</h1>

<p>This sample code demonstrate how you can 
process a refund on a sale transaction created 
using the Payments API.
API used: /v1/payments/sale/{sale-id}/refund</p></div></div><div class="code"><div class="wrapper"><span class="k">require</span> <span class="nx">__DIR__</span> <span class="o">.</span> <span class="s1">&#39;/../bootstrap.php&#39;</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">PayPal\Api\Amount</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">PayPal\Api\Refund</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">PayPal\Api\Sale</span><span class="p">;</span></div></div></div><div class="segment"><div class="comments"><div class="wrapper"><h3 id="refund-amount">Refund amount</h3>

<p>Includes both the refunded amount (to Payer) 
and refunded fee (to Payee). Use the $amt->details
field to mention fees refund details.</p></div></div><div class="code"><div class="wrapper"><span class="nv">$amt</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Amount</span><span class="p">();</span>
<span class="nv">$amt</span><span class="o">-&gt;</span><span class="na">setCurrency</span><span class="p">(</span><span class="s1">&#39;USD&#39;</span><span class="p">)</span>
  <span class="o">-&gt;</span><span class="na">setTotal</span><span class="p">(</span><span class="s1">&#39;0.01&#39;</span><span class="p">);</span></div></div></div><div class="segment"><div class="comments"><div class="wrapper"><h3 id="refund-object">Refund object</h3></div></div></div><div class="segment"><div class="code"><div class="wrapper"><span class="nv">$refund</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Refund</span><span class="p">();</span>
<span class="nv">$refund</span><span class="o">-&gt;</span><span class="na">setAmount</span><span class="p">(</span><span class="nv">$amt</span><span class="p">);</span>

<span class="nv">$saleId</span> <span class="o">=</span> <span class="s1">&#39;3RM92092UW5126232&#39;</span><span class="p">;</span></div></div></div><div class="segment"><div class="comments"><div class="wrapper"><h3 id="sale">Sale</h3>

<p>A sale transaction.
Create a Sale object with the
given sale transaction id.</p></div></div><div class="code"><div class="wrapper"><span class="nv">$sale</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Sale</span><span class="p">();</span>
<span class="nv">$sale</span><span class="o">-&gt;</span><span class="na">setId</span><span class="p">(</span><span class="nv">$saleId</span><span class="p">);</span>
<span class="k">try</span> <span class="p">{</span> </div></div></div><div class="segment"><div class="comments"><div class="wrapper"><p>Refund the sale
(See bootstrap.php for more on <code>ApiContext</code>)</p></div></div><div class="code"><div class="wrapper">  <span class="nv">$sale</span><span class="o">-&gt;</span><span class="na">refund</span><span class="p">(</span><span class="nv">$refund</span><span class="p">,</span> <span class="nv">$apiContext</span><span class="p">);</span>
<span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">PayPal\Exception\PPConnectionException</span> <span class="nv">$ex</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">echo</span> <span class="s2">&quot;Exception:&quot;</span> <span class="o">.</span> <span class="nv">$ex</span><span class="o">-&gt;</span><span class="na">getMessage</span><span class="p">()</span> <span class="o">.</span> <span class="nx">PHP_EOL</span><span class="p">;</span>
  <span class="nb">var_dump</span><span class="p">(</span><span class="nv">$ex</span><span class="o">-&gt;</span><span class="na">getData</span><span class="p">());</span>
  <span class="k">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
<span class="p">}</span>
<span class="cp">?&gt;</span><span class="x"></span>
<span class="x">&lt;html&gt;</span>
<span class="x">&lt;head&gt;</span>
<span class="x">  &lt;title&gt;Refund a sale&lt;/title&gt;</span>
<span class="x">&lt;/head&gt;</span>
<span class="x">&lt;body&gt;</span>
<span class="x">  &lt;div&gt;Refunding sale id: </span><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nv">$saleId</span><span class="p">;</span><span class="cp">?&gt;</span><span class="x">&lt;/div&gt;</span>
<span class="x">  &lt;pre&gt;</span><span class="cp">&lt;?php</span> <span class="nb">var_dump</span><span class="p">(</span><span class="nv">$sale</span><span class="p">);</span><span class="cp">?&gt;</span><span class="x">&lt;/pre&gt;</span>
<span class="x">  &lt;a href=&#39;../index.html&#39;&gt;Back&lt;/a&gt;</span>
<span class="x">&lt;/body&gt;</span>
<span class="x">&lt;/html&gt;</span></div></div></div></div></body></html>